package com.wang.concurrency;

import java.util.concurrent.*;
/**
 * 使用自定义策略的线程池,提交Callable任务
 * ThreadPoolExecutor
 * @author simonwang
 *
 */
public class ThreadPool2 {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		
		ExecutorService pool = getThreadPool();
		Future<Integer> future1 = pool.submit(new ConcreteCallable());
		Future<Integer> future2 = pool.submit(new ConcreteCallable());
		
//		FutureTask<Integer> task = new FutureTask<>(new ConcreteCallable());
//		pool.execute(task);
		
		
		System.out.println(future1.isDone());
		System.out.println(future2.isDone());
		System.out.println(future1.get());
		System.out.println(future2.get());
		pool.shutdown();
	}
	
	
	public static ExecutorService getThreadPool(){
		
		return new ThreadPoolExecutor(5, 20, 20L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
	}
	
	
	private static class ConcreteCallable implements Callable<Integer>{
	
		@Override		
		public Integer call() throws Exception {
			int result = 0;
			for(int i=0;i<100;i++){
				Thread.sleep(10);
				result +=i;
			}
			return result;
		}		
	}
}
